package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Goal;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class GoalDao {
	
	DBHelp<Goal> db = new DBHelp<Goal>();
	public void save(Goal goal) {
		String sql = "INSERT INTO t_goal (id,title,`describe`,createTime,t_project_id,t_user_id,rate) VALUES (?,?,?,?,?,?,?)";
		db.executeSQL(sql, goal.getId(),goal.getTitle(),goal.getDescribe(),goal.getCreateTime(),goal.getProId(),goal.getUserId(),goal.getRate());
	}
	public List<Goal> findByProId(String proId) {
		String sql = "SELECT id,title,`describe`,createTime,t_project_id,t_user_id,rate FROM t_goal WHERE t_project_id=?";
		return db.executeQueryForList(sql, new GoalRowMapper(), proId);
		
	}
	
	private class GoalRowMapper implements RowMapper<Goal> {

		public Goal mapRow(ResultSet rs) throws SQLException {
			Goal goal = new Goal();
			goal.setId(rs.getString("id"));
			goal.setTitle(rs.getString("title"));
			goal.setDescribe(rs.getString("describe"));
			goal.setCreateTime(rs.getLong("createTime"));
			goal.setProId(rs.getString("t_project_id"));
			goal.setUserId(rs.getString("t_user_id"));
			goal.setRate(rs.getShort("rate"));
			return goal;
		}
		
	}

	public Goal findById(String id) {
		String sql = "SELECT id,title,`describe`,createTime,t_project_id,t_user_id,rate FROM t_goal WHERE id=?";
		return db.executeQueryForObject(sql, new GoalRowMapper(), id);
	}
	public void update(String id, String title, String describe) {
		String sql = "UPDATE t_goal SET title=?,`describe`=? WHERE id=?";
		db.executeSQL(sql, title,describe,id);
		
	}
	public void delete(String id) {
		String sql = "DELETE FROM t_goal WHERE id=?";
		db.executeSQL(sql, id);
	}
	
}
